package cn.wanda.bi.sunshine.platform.server.utils;

import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Random;

import cn.wanda.bi.sunshine.platform.server.bean.KeyCommon;

/**
 * created by wangfupeng1
 */
public class PasswordUtil {

    public static String key = "666666666666666666666666666666";
    private static final String[] passwordF={"1","2","3","4","5","6","7","8","9","0","a","b","c","d","e","f"};

    public static String generatePasswordF(int length) {
        String password = "";
        Random random = new Random();
        for (int i = 0; i < length; i++) {
        	password = password+passwordF[random.nextInt(15)];
        }
        password = password.replace("4", "6").replace("d", "f");
        return password;
    }
    
    
    public static String generatePassword(int length) {
        String password = "";
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            password = password + "" + random.nextInt(9);
        }
        password = password.replace("4", "6");
        return password;
    }

    public static String generateToken() {
        String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss").format(Calendar.getInstance().getTime());
        return timeStamp + generatePasswordF(18);
    }

    public static String generateToken(int length) {
        String timeStamp = new SimpleDateFormat("yyyyMMdd").format(Calendar.getInstance().getTime());
        return timeStamp + generatePasswordF(length-8>0?length-8:0);
    }
    
    public static String MD5LowerCaseWithExtraKey(String s) {
        return MD5(s + key).toLowerCase();
    }


    public static String MD5LowerCase(String s) {
        return MD5(s).toLowerCase();
    }
    
    public static String MD5(String s) {
        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        try {
            byte[] btInput = s.getBytes();
            // 获得MD5摘要算法的 MessageDigest 对象  
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // 使用指定的字节更新摘要  
            mdInst.update(btInput);
            // 获得密文  
            byte[] md = mdInst.digest();
            // 把密文转换成十六进制的字符串形式  
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    public static String encryptPasswordBASE64(String password) {
        if (password.isEmpty()) {
            return password;
        }

        DesUtil desUtil = new DesUtil(KeyCommon.BASE64_DES_KEY);

        //密码需要加密才能和数据库的匹配上
        String passwordEncrypt = null;
        try {
            passwordEncrypt = desUtil.encrypt(password);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return passwordEncrypt;
    }
    
}
